import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:ball_lion/entity/other/error_info_entity.dart';
import 'package:ball_lion/resources/colours.dart';
import 'package:ball_lion/resources/styles.dart';
import 'package:ball_lion/utils/sp_utils.dart';
import 'package:ball_lion/utils/toast_utils.dart';
import 'package:ball_lion/utils/utils.dart';
import 'package:ball_lion/widgets/card.dart';
import 'package:ball_lion/widgets/base/wqs_button.dart';
import 'package:ball_lion/widgets/base/wqs_dialog.dart';

import 'package:ball_lion/widgets/base/wqs_text.dart';

class ErrorPage extends StatefulWidget {
  const ErrorPage({super.key});

  @override
  State<ErrorPage> createState() => _ErrorPageState();
}

class _ErrorPageState extends State<ErrorPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colours.greyF2F3F7,
      appBar: Styles.appBar(
          title: QsText("错误列表(${SpUtils.errorInfo?.length ?? 0})"),
          actions: [
            IconButton(
                onPressed: () {
                  SpUtils.showErrorToast = !SpUtils.showErrorToast;
                  Toast.text(SpUtils.showErrorToast ? "打开错误提示" : "关闭错误提示");
                  setState(() {});
                },
                icon: Icon(SpUtils.showErrorToast
                    ? Icons.radio_button_checked
                    : Icons.radio_button_off)),
            IconButton(
                onPressed: () async {
                  var result = await Get.dialog(QsDialog.alert("要删除所有记录吗？"));
                  if (result == true) SpUtils.deleteErrorInfo();
                  setState(() {});
                },
                icon: const Icon(Icons.delete))
          ]),
      body: ListView(
          children: SpUtils.errorInfo?.reversed
                  .map(
                      (e) => errorItem(ErrorInfoEntity.fromJson(jsonDecode(e))))
                  .toList()
                  .append(Container(height: 50)) ??
              []),
    );
  }

  Widget errorItem(ErrorInfoEntity entity) {
    return Block(
      cardStyle: const CardStyle(
          margin: EdgeInsets.fromLTRB(8, 8, 8, 0),
          padding: EdgeInsets.symmetric(horizontal: 10, vertical: 6)),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          if (entity.path.hasValue) QsText("路径：${entity.path ?? ""}"),
          Container(height: 6),
          if (entity.stack.hasValue) QsText("堆栈：${entity.stack ?? ""}"),
          Container(height: 6),
          QsText("报错: ${entity.exception ?? " "}",
              maxLines: 5, color: Colours.grey66),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              QsText(entity.time ?? ""),
              QsButton(
                textStyle:
                    const TextStyle(fontSize: 12, fontWeight: FontWeight.w400),
                backgroundColor: Colours.main,
                foregroundColor: Colours.white,
                borderRadius: 4,
                height: 24,
                padding: const EdgeInsets.symmetric(horizontal: 4, vertical: 4),
                onPressed: () {
                  Clipboard.setData(ClipboardData(text: entity.toString()));
                  Toast.text("已复制");
                },
                text: "复制到剪贴板",
              )
            ],
          ).paddingOnly(top: 4)
        ],
      ),
    );
  }
}
